<?php

namespace App\Admin\Controllers;

use App\Models\LogBalance;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class LogBalanceController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.log_balance'))
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.log_balance'))
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.log_balance'))
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.log_balance'))
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new LogBalance);
        $grid->model()->orderBy('created_at', 'desc');
        $grid->model()->select('log_balance.*', 'users.username')->join('users', 'user_id', 'users.id')->join('wallets', 'wallets.user_id', 'users.id');
        $grid->id('Id');
        $grid->username(trans('admin.username'));
        $grid->user_balance(trans('admin.balance'));
        $grid->action(trans('admin.action'))->display(function ($value) {
            return LogBalance::$ACTION_MAP[$value];
        });
        $grid->amount(trans('admin.amount'));
        $grid->remarks(trans('admin.remarks'));
        $grid->created_at(trans('admin.created_at'))->sortable();
//        $grid->updated_at(trans('admin.updated_at'));
        $grid->disableCreateButton();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
//            $actions->disableView();
            $actions->disableEdit();
//            $actions->disableDelete();
        });
        $grid->filter(function ($filter) {
            $filter->column(1 / 3, function ($filter) {
                $filter->between('user_balance', trans('admin.balance'));
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->like('nick_name', trans('admin.nick_name'));
                $filter->between('amount', trans('admin.amount'));
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->like('username', trans('admin.username'));
                $filter->in('action', trans('admin.action'))->checkbox(LogBalance::$ACTION_MAP);
            });
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(LogBalance::findOrFail($id));

        $show->id('Id');
        $show->user_id(trans('admin.username'));
        $show->action(trans('admin.action'));
        $show->amount(trans('admin.amount'));
        $show->remarks(trans('admin.remarks'));
        $show->created_at(trans('admin.created_at'));
        $show->updated_at(trans('admin.updated_at'));
        $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
//                $tools->disableList();
                $tools->disableDelete();
            });;

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new LogBalance);

        $form->number('user_id', trans('admin.username'));
        $form->text('action', trans('admin.action'));
        $form->decimal('amount', trans('admin.amount'));
        $form->text('remarks', trans('admin.remarks'));
        $form->disableEditingCheck();

        $form->disableCreatingCheck();

        $form->disableViewCheck();

        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
//            $tools->disableView();
//            $tools->disableList();
        });

        return $form;
    }
}
